﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class ProjectItemInfoController : Controller
    {
        readonly IProjectItemInfoServices _itemInfo;
        readonly IProjectItemImageInfoServices _itemImage;
        readonly IUserServices _user;
        public ProjectItemInfoController(IProjectItemInfoServices itemInfo, IUserServices user, IProjectItemImageInfoServices itemImage)
        {
            _itemInfo = itemInfo;
            _user = user;
            _itemImage = itemImage;
        }

        [HttpPost, Route("Insert")]
        public ApiResult Insert([FromBody] ProjectItemInfoDto data)
        {
            if (data == null || string.IsNullOrWhiteSpace(data.ItemName) || data.ProjectId == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            string token = Request.GetToken();
            if (string.IsNullOrWhiteSpace(token))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(token, out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            data.CreateUser = userId;
            if (_itemInfo.HasExistItemName(data.ProjectId, data.ItemName))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PROJECT_ITEM_NAME_EXIST);
            }
            if (!_itemInfo.Insert(data))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Update")]
        public ApiResult Update([FromHeader(Name = "Authorization")] string auth, [FromBody] ProjectItemInfoDto data)
        {
            if (data == null || string.IsNullOrWhiteSpace(data.ItemName) || data.ProjectId == 0 || data.Id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(auth.ReplaceBearer(), out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_itemInfo.HasExistItemId(data.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (_itemInfo.HasExistItemName(data.ProjectId, data.ItemName, data.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PROJECT_ITEM_NAME_EXIST);
            }
            if (!_itemInfo.Update(data, userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }

            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Delete")]
        public ApiResult Delete([FromQuery] int id)
        {
            if (id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_itemInfo.HasExistItemId(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_itemInfo.Delete(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("GetAllByProjectId")]
        public ApiResult GetAllByProjectId([FromBody] Models.PageBo<ProjectItemInfoParams> pageBo)
        {
            if (pageBo == null || pageBo.Param == null || pageBo.Param.ProjectId == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            var result = _itemInfo.GetByProjectId(pageBo);
            return ApiResultStatus.Success(result);
        }

        [HttpPost,Route("UpdateImages")]
        public ApiResult UpdateImages([FromBody] UpdateProjectItemImageRequest reqData) 
        {
            if (reqData == null || reqData.ItemId == 0) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (reqData.ItemImages == null || reqData.ItemImages.Count == 0) 
            {
                return ApiResultStatus.Create(_itemImage.Delete(reqData.ItemId), ApiResultCodes.OPERATION_FAILED);
            }

            string token = Request.GetToken();
            if (string.IsNullOrWhiteSpace(token))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(token, out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            return ApiResultStatus.Create(_itemImage.AddOrUpdate(reqData.ItemImages, userId, reqData.ItemId), ApiResultCodes.OPERATION_FAILED);
        }

        [HttpGet, Route("GetProjectItemInfoByManager")]
        public ApiResult GetProjectItemInfoByManager([FromQuery] int itemId)
        {
            if (itemId == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            return ApiResultStatus.Success(_itemInfo.GetProjectItemInfoByManager(itemId));
        }

        [HttpGet, Route("GetProjectItemInfoByFinancer")]
        public ApiResult GetProjectItemInfoByFinancer([FromQuery] int itemId)
        {
            if (itemId == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            return ApiResultStatus.Success(_itemInfo.GetProjectItemInfoByFinancer(itemId));
        }
    }
}
